package com.etb.wisdom.eframe.rights.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.etb.core.exception.ETBServiceException;
import com.etb.core.service.ETBService;
import com.etb.core.utils.DataUtil;
import com.etb.core.utils.EncryptionUtil;
import com.etb.wisdom.eframe.auth.AuthConstant.AuthType;
import com.etb.wisdom.eframe.auth.AuthConstant.PartyType;
import com.etb.wisdom.eframe.model.EtbPartyAuth;
import com.etb.wisdom.eframe.model.EtbUser;
import com.etb.wisdom.eframe.model.OrgEmployee;
import com.etb.wisdom.eframe.rights.service.EtbUserService;
import com.etb.wisdom.eframe.system.constant.SecurityConstant;

@Service
public class EtbUserServiceImpl extends ETBService implements EtbUserService {

	@Override
	public void saveRole(String userId, String addRoleIds, String delRoleIds) {
		List<String> addIdList = DataUtil.split(addRoleIds);
		List<String> delIdList = DataUtil.split(delRoleIds);
		for(String id: addIdList) {
			EtbPartyAuth auth = new EtbPartyAuth();
			auth.setPartyid(userId);
			auth.setPartytype(PartyType.user.toString());
			auth.setAuthid(id);
			auth.setAuthtype(AuthType.role.toString());
			service.save(auth);
		}
		for(String id: delIdList) {
			EtbPartyAuth auth = new EtbPartyAuth();
			auth.setPartyid(userId);
			auth.setPartytype(PartyType.user.toString());
			auth.setAuthid(id);
			auth.setAuthtype(AuthType.role.toString());
			service.delete(auth);
		}
	}
	
	@Override
	public void testTransaction() {
//		EtbUser user = new EtbUser();
//		user.setLoginname("test");
//		service.save(user);
//		
//		
//		Map<String, Object> flowCommandInfo = new HashMap<String, Object>();
//		flowCommandInfo.put("processDefinitionKey", "test_1");
//		flowCommandInfo.put("commandId", "HandleCommand_3");
//		flowCommandInfo.put("commandType", "startandsubmit");
//		flowCommandInfo.put("bizKey", "baozhangfangyewu123");
//		flowCommandInfo.put("processInstanceId", "");
//		Map<String, Object> formData = new HashMap<String, Object>();
//		formData.put("ownerName", "超级管理员");
//		formData.put("invoiceType", "0");
//		formData.put("owner", "admin");
//		formData.put("account", "1111");
//		formData.put("dept", "200012");
//		formData.put("expenseId", "baozhangfangyewu123");
//		formData.put("flowCommandInfo", flowCommandInfo);
//		ExpandTaskCommand command = new ExpandTaskCommand();
//		command.setProcessDefinitionKey("test_1");
//		command.setBusinessKey("baozhangfangyewu123");
//		command.setCommandType("startandsubmit");
//		command.setTaskCommandId("HandleCommand_3");
////		command.setTransientVariables(formData);
//		Authentication.setAuthenticatedUserId("admin2");
//		
//		TaskService task = FoxBpmUtil.getProcessEngine().getTaskService();
//		task.expandTaskComplete(command, Object.class);
	}

	@Override
	public void save(EtbUser user) {
		if(user.getId() == null) {
			if(StringUtils.isBlank(user.getPassword())) {
				user.setPassword(EncryptionUtil.encrypt(SecurityConstant.DEFAULT_PASSWORD));
			} else {
				user.setPassword(EncryptionUtil.encrypt(user.getPassword()));
			}
		} else {
			OrgEmployee emp = new OrgEmployee();
			emp.setUserid(user.getLoginname());
			service.extend(emp);
			if(emp.getId() != null) {
				emp.setEmpname(user.getUsername());
			}
			service.save(emp);
		}
		service.save(user);
	}
	
	@Override
	public void resetPassword(List<EtbUser> dataObjects) {
		for(EtbUser user: dataObjects) {
			service.extend(user);
			user.setPassword(SecurityConstant.DEFAULT_PASSWORD);
			service.save(user);
		}
	}
	
	@Override
	public void saveNewPassword(String loginname, String oldPassword, String newPassword) {
		EtbUser user = new EtbUser();
		user.setLoginname(loginname);
		service.extend(user);
		if(EncryptionUtil.encrypt(oldPassword).equals(user.getPassword())) {
			user.setPassword(EncryptionUtil.encrypt(newPassword));
			service.save(user);
		} else {
			throw new ETBServiceException("在修改密码时输入的原始密码不匹配");
		}
	}

}
